package com.lgh.springframework.beans.factory.support;

import com.lgh.springframework.beans.BeansException;
import com.lgh.springframework.beans.factory.config.BeanDefinition;

import java.lang.reflect.Field;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname AbstractAutowireCapableBeanFactory
 * @description 创建bean对象的实现
 * @date 2023/02/21 20:56
 * <p>
 * ******************************************************
 */
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory {

    @Override
    protected Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException {
        Object bean = null;
        try {
            bean = beanDefinition.getBeanClass().newInstance();
            // 无参bean，有参该如何处理
            /*Field[] declaredFields = beanDefinition.getBeanClass().getDeclaredFields();
            boolean accessible = declaredFields[0].isAccessible();
            if (!accessible) {
                declaredFields[0].setAccessible(true);
                declaredFields[0].set(bean, "aaa");
                declaredFields[0].setAccessible(false);
            }*/
        } catch (InstantiationException | IllegalAccessException e) {
            throw new BeansException("Instantiation of bean failed", e);
        }
        addSingleton(beanName, bean);
        return bean;
    }
}
